package org.misty.seckill.config;

import org.misty.seckill.interceptor.AuthorizationInterceptor2;
import org.misty.seckill.resolver.UserArgumentResolver;
import org.misty.seckill.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@Order(2)
public class WebConfigurer implements WebMvcConfigurer {
  @Autowired
  private UserArgumentResolver resolver;

//  @Autowired
//  private PreprocessInterceptor preprocessInterceptor;

//  @Autowired
//  private AuthorizationInterceptor authorizationInterceptor;

  @Autowired
  private AuthorizationInterceptor2 authorizationInterceptor2;

  @Bean(name = "isAuthorizationEnable")
  public boolean isAuthorizationEnable() {
    return true;
  }

  @Bean
  public SnowflakeIdWorker snowflake() {
    return new SnowflakeIdWorker(0, 0);
  }

  @Override
  public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
    resolvers.add(resolver);
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
//    registry.addInterceptor(new SampleInterceptor1());
//    registry.addInterceptor(new SampleInterceptor2());
    /*registry.addInterceptor(authorizationInterceptor)
        .addPathPatterns("/sample")
        .addPathPatterns("/sample/**");*/

//    registry.addInterceptor(preprocessInterceptor);
//    registry.addInterceptor(authorizationInterceptor)
//        .excludePathPatterns("/sample")
//        .excludePathPatterns("/favicon.ico")
//        .excludePathPatterns("/error")
//        .excludePathPatterns("/login", "/login/*")
//        .excludePathPatterns("/goods", "/goods/page*")
//        .excludePathPatterns("/**/*.html", "/**/*.js");

    registry.addInterceptor(authorizationInterceptor2)
        .excludePathPatterns("/**/*.html", "/**/*.js", "/**/*.css", "/**/*.ico")
        .excludePathPatterns("/error");
  }
}
